
#include "globals.h"

int cpolygon;					// current polygon variable
int INC_VALUE;					// The value by which the tube rotates
int cur_i;						// the current number of recursions
float cur_r;					// scaling value
bool useratio;					// whether to use the schlicker ratios
bool iterative;					// whether to use the iterative method
float x_viewpos, y_viewpos, z_viewpos;
float x_rotdir, y_rotdir, z_rotdir;
int polygonmode, pointsize, pointsmooth;
int coloring_mode, colortheme;
float rcolor, gcolor, bcolor;
bool change;
int cycles;

void InitialValues()
{
	cpolygon = 5;
	INC_VALUE = 5;
	cur_i = 5;
	cur_r = 2.0f;
	useratio = true;
	iterative = true;

			// The viewing positions
	x_viewpos = 0.0f;
	y_viewpos = 0.0f;
	z_viewpos = 3.0f;

		// The default rotation directions
	x_rotdir = 1.0f;
	y_rotdir = 0.8f; 
	z_rotdir = 0.6f;

		// Wireframe or solid
	polygonmode = 2;
	pointsize = 3;
	pointsmooth = TRUE;

		// The mode of coloring the tubes
	coloring_mode = 0;
	colortheme = 4;
	rcolor = 1.0;
	gcolor = 1.0;
	bcolor = 1.0;

	change = true;
	cycles = 1;
}

/* Global variables for the GetValue and SetValue functions. Registry
   is opened, and these functions are called. */

static HKEY SettingsKey;
static DWORD result, size;

/* These are the registry keys that are used to save and query
   values out of the system registry. */
static char KEY[] = "Software\\Mochan\\3D Sierpinski Polyhedra";

static char CURRENT_POLYGON[] = "Current Polygon";
static char SPEED[] = "Speed";
static char I[] = "I";
static char R[] = "R";
static char USERATIO[] = "Use Ratio";
static char ITERATIVE[] = "Iterative";

static char XPOS[] = "XPos";
static char YPOS[] = "YPos";
static char ZPOS[] = "ZPos";

static char XROTDIR[] = "XRotDir";
static char YROTDIR[] = "YRotDir";
static char ZROTDIR[] = "ZRotDir";

static char POLMODE[] = "PolMode";
static char PTSIZE[]  = "PtSize";
static char PSMOOTH[] = "PSmooth";

static char COLORINGMODE[] = "ColoringMode";
static char COLORTHEME[] = "ColorTheme";
static char RCOLOR[] = "RColor";
static char GCOLOR[] = "GColor";
static char BCOLOR[] = "BColor";

static char CHANGE[] = "Change";
static char CYCLES[] = "Cycles";

/* Get a value into the registry with the key and store it in
   value */
void GetValue (char* key, char* value)
{
	size = sizeof(DWORD);
	RegQueryValueEx (SettingsKey, key, NULL, &result, 
		(unsigned char*)value, &size);
}

/* Set a value into the registry from the key given and store the 
   passed value.*/
void SetValue (char* key, unsigned char* value)
{
	RegSetValueEx (SettingsKey, key, 0, REG_DWORD, value,
		sizeof(DWORD));
}

/* Open a registry and get the key to retrieve the value and save
   the value */
void OpenKey ()
{
	RegCreateKeyEx (HKEY_CURRENT_USER, KEY,
		0, NULL, 0, KEY_ALL_ACCESS, NULL, &SettingsKey, &result);
}

/* Close the registry key as we are done with it */
void CloseKey ()
{
	RegCloseKey (SettingsKey);
}

/* Load all the settings for the program to run. If the settings do
   not exist, then the default values are used. */
void LoadSettings ()
{
	InitialValues();

	OpenKey();

	GetValue (CURRENT_POLYGON, (char*)&cpolygon);
	GetValue (SPEED, (char*)&INC_VALUE);
	GetValue (I, (char*)&cur_i);
	GetValue (R, (char*)&cur_r);
	GetValue (USERATIO, (char*)&useratio);
	GetValue (ITERATIVE, (char*)&iterative);

	GetValue (XPOS, (char*)&x_viewpos);
	GetValue (YPOS, (char*)&y_viewpos);
	GetValue (ZPOS, (char*)&z_viewpos);
	
	GetValue (XROTDIR, (char*)&x_rotdir);
	GetValue (YROTDIR, (char*)&y_rotdir);
	GetValue (ZROTDIR, (char*)&z_rotdir);

	GetValue (POLMODE, (char*)&polygonmode);
	GetValue (PTSIZE, (char*)&pointsize);
	GetValue (PSMOOTH, (char*)&pointsmooth);

	GetValue (COLORINGMODE, (char*)&coloring_mode);
	GetValue (COLORTHEME, (char*)&colortheme);
	GetValue (RCOLOR, (char*)&rcolor);
	GetValue (GCOLOR, (char*)&gcolor);
	GetValue (BCOLOR, (char*)&bcolor);

	GetValue (CHANGE, (char*)&change);
	GetValue (CYCLES, (char*)&cycles);

	CloseKey();
}

/* Save the values of the elements that pertain to the General tab
   of the settings. */
void SaveGeneralSettings ()
{
	OpenKey();

	SetValue (CURRENT_POLYGON, (unsigned char*)&cpolygon);
	SetValue (SPEED, (unsigned char*)&INC_VALUE);
	SetValue (I, (unsigned char*)&cur_i);
	SetValue (R, (unsigned char*)&cur_r);
	SetValue (USERATIO, (unsigned char*)&useratio);
	SetValue (ITERATIVE, (unsigned char*)&iterative);

	SetValue (CHANGE, (unsigned char*)&change);
	SetValue (CYCLES, (unsigned char*)&cycles);

	CloseKey();
}

/* Save the values of the elements that pertain to the Viewing tab
   of the settings. */
void SaveViewingSettings ()
{
	OpenKey();

	SetValue (XPOS, (unsigned char*)&x_viewpos);
	SetValue (YPOS, (unsigned char*)&y_viewpos);
	SetValue (ZPOS, (unsigned char*)&z_viewpos);

	SetValue (XROTDIR, (unsigned char*)&x_rotdir);
	SetValue (YROTDIR, (unsigned char*)&y_rotdir);
	SetValue (ZROTDIR, (unsigned char*)&z_rotdir);

	CloseKey();
}

void SaveRenderingSettings ()
{
	OpenKey();

	SetValue (POLMODE, (unsigned char*)&polygonmode);
	SetValue (PTSIZE, (unsigned char*)&pointsize);
	SetValue (PSMOOTH, (unsigned char*)&pointsmooth);

	CloseKey();
}

void SaveColoringSettings ()
{
	OpenKey();

	SetValue (COLORINGMODE, (unsigned char*)&coloring_mode);
	SetValue (COLORTHEME, (unsigned char*)&colortheme);
	SetValue (RCOLOR, (unsigned char*)&rcolor);
	SetValue (GCOLOR, (unsigned char*)&gcolor);
	SetValue (BCOLOR, (unsigned char*)&bcolor);

	CloseKey();
}